#include <stdio.h>
#include "list.h"
#include "unix_api_adapter.h"
#include "cache_pool.h"

int cmp(void *a, void *b) {
	return (int)a >= (int)b ? 1 : -1;
}

struct cache_pool *pool;

struct task_struct {
	char *name;
	int k;
	int c;
};

struct task_struct* cache_mem_alloc() {
	if (0 == list_length(pool->cache_list)) {
		struct task_struct *task1 = (struct task_struct *)malloc(sizeof(struct task_struct));
		task1->name = "lqkweafaef";
		task1->k = 23;
		task1->c = 888;
		cache_put(pool, task1);
		return task1;
	}
	else {
		struct task_struct * ret = (struct task_struct*)cache_get(pool);
		return ret;
	}
}

void cache_mem_free(struct task_struct *task) {
	//dummy for test..

}

int main()
{
	pool = (struct cache_pool*)malloc(sizeof(struct cache_pool));
	pool->cache_list = (struct list*)malloc(sizeof(struct list));
	
	cache_init(pool, "task_struct");
	struct task_struct* task = cache_mem_alloc();
	printf(task->name);
	//struct list l;
	//list_init(&l);
	//list_add_tail(&l, 234);
	//list_add_tail(&l, -999);
	//for (int i = 0; i < 1000; i++) {
	//	list_add_tail(&l, 20 - i);
	//}
	//list_add_tail(&l, -34);

	//struct list *cur;
	//list_reverse(&l);
	//list_sort(&l, cmp);
	//int socketfd = socket_create();
	//struct socket_address saddr;
	//saddr.ip_address = "127.0.0.1";
	//saddr.port = 19999;
	//if (socket_bind(socketfd, &saddr) == -1) {
	//	printf("bind error");
	//	return;
	//}
	//printf("try to listen....\n");
	//int listen_socketfd = socket_listen(socketfd);
	//if (listen_socketfd == -1) {
	//	printf("listen failed");
	//	return;
	//}
	//
	//char buf[1024];
	//while (1) {
	//	struct socket_address saddr;
	//	printf("try to accept....\n");
	//	printf("will waiting for connection...\n");
	//	int accept_socketfd = socket_accept(socketfd, &saddr);
	//	if (accept_socketfd == -1) {
	//		printf("accept failed");
	//		return;
	//	}
	//	printf("received connection\n");
	//	int len = socket_recv(accept_socketfd, buf, 1024);
	//	buf[len] = '\n';
	//	printf("received msg from %s: %s", saddr.ip_address, buf);
	//}




}